//
//  XZEditVC.m
//  Teecloud
//
//  Created by PC on 2018/11/7.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZEditVC.h"
#import "ZJPickBaseView.h"
#import "ZJDatePickerView.h"
#import "ZJNormalPickerView.h"
@interface XZEditVC ()
//事由
@property (weak, nonatomic) IBOutlet UILabel *reasonLabel;
//开始时间
@property (weak, nonatomic) IBOutlet UILabel *starTimeLabel;
//结束时间
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;
//截止时长
@property (weak, nonatomic) IBOutlet UILabel *loseTimeLabel;
//应到人数
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
//签到开关
@property (weak, nonatomic) IBOutlet UISwitch *signinSwitch;
//签退开关
@property (weak, nonatomic) IBOutlet UISwitch *signoutSwitch;
//截止时长 人数
@property (nonatomic, copy) NSString *loseTime;
@property (nonatomic, copy) NSString *count;
//签到/签退
@property (nonatomic, assign) BOOL IsNeedSignIn;
@property (nonatomic, assign) BOOL IsNeedSignOut;
@end

@implementation XZEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.signinSwitch.on = YES;
    self.IsNeedSignIn = YES;
    //编辑二维码要根据model来绑定表单
    if (self.model != nil ) {
        self.reasonLabel.text = self.model.Name;
        self.starTimeLabel.text = self.model.StartTime;
        self.endTimeLabel.text = self.model.EndTime;
        self.loseTimeLabel.text = [NSString stringWithFormat:@"%@ 分钟",self.model.LoseTime];
        //截止时长
        self.loseTime = [NSString stringWithFormat:@"%@",self.model.LoseTime];
        //截止时长
        self.count = [NSString stringWithFormat:@"%@",self.model.Count];
        
        self.countLabel.text = [NSString stringWithFormat:@"%@人",self.model.Count];
        //是否需要签退
        self.signoutSwitch.on = self.model.IsNeedSignOut;
        self.IsNeedSignOut = self.model.IsNeedSignOut;
    }
}
//事由
- (IBAction)reasonTapAction:(UITapGestureRecognizer *)sender {
    UIAlertController *addAlertVC = [UIAlertController alertControllerWithTitle:@"请输入签到事由" message:nil preferredStyle:UIAlertControllerStyleAlert];
    //创建textFiled输入框
    [addAlertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        if (!kIsEmptyString(self.model.Name)) {
            textField.text = self.model.Name;
        }else{
            textField.placeholder = @"请输入...";
        }
    }];
    //创建取消事件(按钮)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [addAlertVC addAction:cancelAction];
    kWeakObject(self)
    UIAlertAction *confirmAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        NSString *reasonName = addAlertVC.textFields.firstObject.text;
        weakself.reasonLabel.text = reasonName;
    }];
    [addAlertVC addAction:confirmAction];
    [self.navigationController presentViewController:addAlertVC animated:NO completion:nil];
}
//开始时间
- (IBAction)starTimeTapAction:(UITapGestureRecognizer *)sender {
    kWeakObject(self)
    [ZJDatePickerView zj_showDatePickerWithTitle:@"请选择开始时间" dateType:ZJDatePickerModeYMDHM defaultSelValue:self.model.StartTime minDate:nil maxDate:nil isAutoSelect:NO lineColor:kDarkGrayColor rowHeight:40 resultBlock:^(NSString *selectValue) {
        weakself.starTimeLabel.text = selectValue;
        self.model.StartTime = selectValue;
    } cancelBlock:^{
        
    }];
}
//结束时间
- (IBAction)endTimeTapAction:(UITapGestureRecognizer *)sender {
    if ([self.starTimeLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showErrorOrSuc:@"请先选择开始时间" toView:self.view];
        return;
    }
    kWeakObject(self)
    [ZJDatePickerView zj_showDatePickerWithTitle:@"请选择结束时间" dateType:ZJDatePickerModeYMDHM defaultSelValue:self.model.EndTime minDate:nil maxDate:nil isAutoSelect:NO lineColor:kDarkGrayColor rowHeight:40 resultBlock:^(NSString *selectValue) {
        NSDate *starDate = [weakself StringTODate:weakself.starTimeLabel.text];
        NSDate *endDate = [weakself StringTODate:selectValue];
        
        BOOL isTrue = [weakself isSameDay:starDate date2:endDate];
        if (!isTrue) {
            [MBProgressHUD showErrorOrSuc:@"结束时间跟开始时间须为同一天" toView:weakself.view];
        }else{
            weakself.endTimeLabel.text = selectValue;
            self.model.EndTime = selectValue;
        }
        
    } cancelBlock:^{
        
    }];
}
//截止时长
- (IBAction)loseTimeTapAction:(UITapGestureRecognizer *)sender {
    UIAlertController *addAlertVC = [UIAlertController alertControllerWithTitle:@"请输入截止时长" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    //创建textFiled输入框
    [addAlertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        if (self.model.LoseTime != nil) {
            textField.text = [NSString stringWithFormat:@"%@",self.model.LoseTime];;
        }else{
           textField.placeholder = @"请输入...";
        }
        
    }];
    //创建取消事件(按钮)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [addAlertVC addAction:cancelAction];
    kWeakObject(self)
    UIAlertAction *confirmAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        NSString *loseTime = addAlertVC.textFields.firstObject.text;
        weakself.loseTime = loseTime;
        weakself.loseTimeLabel.text = [NSString stringWithFormat:@"%@ 分钟",loseTime];;
    }];
    [addAlertVC addAction:confirmAction];
    [self.navigationController presentViewController:addAlertVC animated:NO completion:nil];
}
//人数
- (IBAction)countTapAction:(UITapGestureRecognizer *)sender {
    UIAlertController *addAlertVC = [UIAlertController alertControllerWithTitle:@"请输入应到人数" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    //创建textFiled输入框
    [addAlertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        if (self.model.Count != nil) {
            textField.text = [NSString stringWithFormat:@"%@",self.model.Count];
        }else{
            textField.placeholder = @"请输入...";
        }
    }];
    //创建取消事件(按钮)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [addAlertVC addAction:cancelAction];
    kWeakObject(self)
    UIAlertAction *confirmAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //应到人数
        NSString *count = addAlertVC.textFields.firstObject.text;
        weakself.count = count;
        weakself.countLabel.text = [NSString stringWithFormat:@"%@人",count];
    }];
    [addAlertVC addAction:confirmAction];
    [self.navigationController presentViewController:addAlertVC animated:NO completion:nil];
    
}
//签到
- (IBAction)signinSwithAction:(UISwitch *)sender {
    self.IsNeedSignIn = sender.on;
}
//签退
- (IBAction)signoutSwitchAction:(UISwitch *)sender {
    self.IsNeedSignOut = sender.on;
}


//提交
- (IBAction)submitButtonAction:(UIButton *)sender {
    if ([self.reasonLabel.text isEqualToString:@"请输入"]) {
        [MBProgressHUD showErrorOrSuc:@"请输入签到事由" toView:self.view];
        return;
    }
    if ([self.starTimeLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showErrorOrSuc:@"请选择开始时间" toView:self.view];
        return;
    }
    
    if ([self.endTimeLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showErrorOrSuc:@"请选择结束时间" toView:self.view];
        return;
    }
    if ([self.loseTimeLabel.text isEqualToString:@"请输入"]) {
        [MBProgressHUD showErrorOrSuc:@"请输入截止时长" toView:self.view];
        return;
    }
    [self submitParameterToServer];
}
//提交参数给服务器
- (void)submitParameterToServer{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    //编辑时有id
    if (self.isEdit) {
         [dict setValue:self.model.Id forKey:@"Id"];
    }
    [dict setValue:self.groupId forKey:@"GroupId"];
    
    [dict setValue:self.reasonLabel.text forKey:@"Name"];
    [dict setValue:self.starTimeLabel.text forKey:@"StartTime"];
    [dict setValue:self.endTimeLabel.text forKey:@"EndTime"];
    [dict setValue:@(self.IsNeedSignIn) forKey:@"IsNeedSignIn"];
    [dict setValue:@(self.IsNeedSignOut) forKey:@"IsNeedSignOut"];
    
    [dict setValue:self.loseTime forKey:@"LoseTime"];
    if (![self.countLabel.text isEqualToString:@"请输入（不输入自动取成员人数）"]) {
        [dict setValue:self.count forKey:@"Count"];
    }
    kWeakObject(self)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=PostSignQrcodeInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                if (weakself.EditBlock) {
                    weakself.EditBlock();
                }
                [weakself.navigationController popViewControllerAnimated:true];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"请求出错" toView:weakself.view];
        }
    }];
}


//字符串转日期
- (NSDate *)StringTODate:(NSString *)string{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm";
    NSDate *data = [format dateFromString:string];
    return data;
}
/**
 *  是否为同一天
 */
- (BOOL)isSameDay:(NSDate*)date1 date2:(NSDate*)date2
{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
    
    return [comp1 day]   == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}




@end
